<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();

chdir("../");
$path3 = getcwd();
chdir(dirname(__FILE__));
#debug($path3 , $path2);
include_once $path3."/lib/core.class.php";



class CustomersList extends IPlugin 
{
    var $color;
    
    function CustomersList($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
        $this->color = "red";
    }
    
    function getPluginTag() { return "customerslist"; }
    function getPluginName() { return "Customers List Plugin"; }
    function getPluginVersion() { return "1.0.0"; }
    
    function getProperty($p = null)
    {
        if(null == $p)
        {
            return array("MenuBlockTitle", "MenuTitle");
        }
        $result = "";
        switch ($p)
        {
            case "MenuBlockTitle": { return "Subscribers manager"; } break;
            case "MenuTitle" : { return "Customers"; } break;
        };
        return $result;
    }

    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------
    function doAction($params)
    {
        if(null != ($result = IPlugin::doAction($params)))
        {
            return $result;
        }
        
        $core = getCoreInstance();
        $arg = $core->getGET();
        
        if("echo" == $arg["action"])
        {
            echo "CustomerList: echo: ".$arg["string"];
        }
        if("doDeleteSubscriber" == $arg["action"] && $arg["id"]>0)
        {
            $db = $core->getProperty("db");
            $db->Exec("delete from subscribe where id={$arg[id]}");
        }

        if("doSubmitSubscriber" == $_POST["action"])
        {
            $z = $_POST;
            $uid = $z["id"];
            unset($z["action"], $z["id"]);

                    $str_mail_groups = "";
                    if (isset($_POST['mail_groups']))
                    {
                        foreach ($_POST['mail_groups'] as $mail_group)
                        {
                            $str_mail_groups .= "<{$mail_group}>";
                        }
                        unset($_POST['mail_groups']);
                    }
                    $str_mail_groups = ", mail_groups = '" . $str_mail_groups . "'";
            $sql = "";
            foreach ($z as $k => $v)
            {
                $sql .= ($sql=="" ? "" : ", ")."{$k} = '".str2db($v)."'";
            }
            $sql .= $str_mail_groups;
        //debug($sql);
            $core = getCoreInstance();
            $db = $core->getProperty("db");
            
            $sql = "update subscribe set {$sql} where id={$uid}";
            //debug($sql );
            $db->Exec($sql);
        }
        
        return true;
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Render pages ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------
    function Render($params)
    {
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }
        
        $core = getCoreInstance();
        $render = $core->framework->smarty;
        $render->assign("baseurl", "?".$this->getURL()."&page=".$_G["page"]);

        $page = "";

        switch ($_G["page"]) {
            case "edit":
                $page = $this->renderCustomerEdit($render);
                break;
            
            default:
                $page = $this->renderCustomerList($render);
                break;
        }

        return $render->fetch("file:".$this->tplfolder."/".$page);
    }


    // ----------------------------------------------------------------------------------
    // --- [ Generate Customers List ] --------------------------------------------------
    // ----------------------------------------------------------------------------------
    
    function cb_rndCL($data)
    {
        $dt = array(
                array($data["id"], "align=\"center\""),
                array($data["fname"]." ".$data["lname"], ""),
                array($data["email"], ""),
                array("<a href=\"?".$this->getURL()."&page=edit&id={$data[id]}\">edit</a>&nbsp;&nbsp;<a href=\"?".$this->getURL()."&action=doDeleteSubscriber&id={$data[id]}\">delete</a>","align=\"center\""),
                );
//      debug($dt, "CBfunction");
        return $dt;
    }

    function renderCustomerList(&$render)
    {
        $core = getCoreInstance();
        $db = $core->getProperty("db");
        $pg = &$core->getSnippet("paginator");

        $params = array(
                    "cb_class" => &$this,
                    "cb_func" => cb_rndCL,
                    "urlbase" => $this->getURL(),
                    "caption" => "Subscribers list",
                    "emptymsg" => "<center>No customers subscribed for emails yet</center>",
                    "header" => array("ID", "Customer Name", "Customer Email", "Actions"),
                    "items" => "",
                    "source" => "sql", // -- sql, array
                    "sql_items" => "select id, fname, lname, email  from subscribe",
                    "sql_count" => "select count(*) from subscribe",
                    "ippg" => 15, // -- items per page
                    "nppv" => 5, // -- number pages per view
                    );

        $HTML = $pg->Render($params);
        $render->assign("customers", $HTML);
        return "customerlist.tpl.php";
    }
    
    
    // ----------------------------------------------------------------------------------
    // --- [ Generate Customers Edit ] --------------------------------------------------
    // ----------------------------------------------------------------------------------

    function renderCustomerEdit(&$render)
    {
        $core = getCoreInstance();
        $db = $core->getProperty("db");
        
        $uid = $_GET["id"];
        if(empty($uid) || $uid<0)
        {
            header("location: ./?".$this->getURL());
            exit();
        }
        
        $render->assign("url", "?".$this->getURL());
        $render->assign("id", $uid);
        $data=$db->getRow("select * from subscribe where id={$uid}");
        $data['mail_groups'] = str_replace("<", "", $data['mail_groups']);
        $data['mail_groups'] = split(">", $data['mail_groups']);


        $render->assign("usr", $data);
        
        //---------------- interest in
        if ($data[isintrest]==0) $isintrest="All"; 
         else $isintrest =$db->getone("select title from catalog where catalog_id=$data[isintrest]");
        $render->assign("isinterest", $isintrest);
        //---------------- how hear about us        
        $hhear=$db->getall("select * from how_hearus");
        $render->assign("hhear", $hhear);

        $sql = "select name, group_id from mail_groups where is_active = 1";
        $list_mail_groups =  $db->getAll($sql);


        $render->assign("list_mail_groups", $list_mail_groups);

        return "customeredit.tpl.php";
    }
};


?>